{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [教程](http://www.naodongopen.com/272.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "      名称\t  作用\n",
    "ndarray.flags\t有关数组的内存布局的信息。\n",
    "ndarray.shape\t数组维数组。\n",
    "ndarray.strides\t遍历数组时，在每个维度中步进的字节数组。\n",
    "ndarray.ndim\t数组维数，在Python世界中，维度的数量被称为rank。\n",
    "ndarray.data\tPython缓冲区对象指向数组的数据的开始。\n",
    "ndarray.size\t数组中的元素总个数。\n",
    "ndarray.itemsize\t一个数组元素的长度（以字节为单位）。\n",
    "ndarray.nbytes\t数组的元素消耗的总字节数。\n",
    "ndarray.base\t如果内存是来自某个其他对象的基本对象。\n",
    "ndarray.dtype\t数组元素的数据类型。\n",
    "ndarray.T\t   数组的转置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(9)\n",
    "b = a.reshape(3, 3)\n",
    "b.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : True\n",
       "  F_CONTIGUOUS : False\n",
       "  OWNDATA : False\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3, 6],\n",
       "       [1, 4, 7],\n",
       "       [2, 5, 8]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([(2, 3, 4), (5, 6, 7)], dtype=np.uint64)\n",
    "a.astype(np.bool_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3], dtype=uint64)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.uint64([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19, 20, 21, 22, 23])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(24)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "a.resize(2, 5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1][::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 7, 9, 2])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10, 0, -1)\n",
    "# a.resize(2,5)\n",
    "a[np.array([3, 3, 1, 8])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5,  6],\n",
       "       [ 7,  8,  9, 10, 11, 12, 13],\n",
       "       [14, 15, 16, 17, 18, 19, 20],\n",
       "       [21, 22, 23, 24, 25, 26, 27],\n",
       "       [28, 29, 30, 31, 32, 33, 34]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(35)\n",
    "a.resize(5, 7)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 等比数列，参数为点个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAD8CAYAAACfF6SlAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAET5JREFUeJzt3X+s3XV9x/HnyxYmooKOO8S2E5JVWGec6AnBkTkyMBY2Kck2hM2Jhtg/FHWbY4GxoMEswbE5Z8bcOsZE5yAdY9rNblWRxWQRw604FBjS1R+9pdjrD9gPcYC+98c9dZe7217uPd97Ts/9PB8JOd/v53z6/by+tHn19Ps959xUFZK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n+arSRlt0UMFVVB/5VdwszfxmsZGcDX66q6ap6HLgV+KkRZxqWryc5AaD/uL/rBVZa+d8JrE9yUpIjmbk5tG3EmZZVkjBzHfi+qnrPqPMMQ1VdUVVrq+pEZn6PP1VVK/oVYVU9BOxJcnJ/6Czg3hFGGoavAacneUb/z/lZrPCb3LNsAy7ub18MfLTrBVZ3fcBRqqonklwK7GDmnQE3VNU9I4613M4AfhX4QpLP98d+u6q2jzCTlsdbgA/3X9jsBt4w4jzLqqo+m+QW4HPMvKvtLlbgVz0kuQk4EzguyRTwDuAaYGuSS5j5avsLOl/Xr3eQpPastMs+kqSnwPKXpAZZ/pLUIMtfkhpk+UtSgyx/SWqQ5S9JDfpfDcdMeA91lQEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "N = 8\n",
    "y = np.zeros(N)\n",
    "x1 = np.linspace(0, 10, N, endpoint=True)\n",
    "x2 = np.linspace(0, 10, N, endpoint=False)\n",
    "plt.plot(x1, y, 'o')\n",
    "plt.plot(x2, y + 0.5, 'o')\n",
    "plt.ylim([-.5, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 等差数列， 参数为点个数, base=10\n",
    "\n",
    "np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\n",
    "\n",
    "``开始：base ** start``"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 100.        ,  215.443469  ,  464.15888336, 1000.        ])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(2.0, 3.0, num=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100.        , 177.827941  , 316.22776602, 562.34132519])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(2.0, 3.0, num=4, endpoint=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.        , 5.0396842 , 6.34960421, 8.        ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(2.0, 3.0, num=4, base=2.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.25892541,  1.58489319,  1.99526231,  2.51188643,  3.16227766,\n",
       "        3.98107171,  5.01187234,  6.30957344,  7.94328235, 10.        ])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0.1, 1, N, endpoint=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAD05JREFUeJzt3V9oXOedxvHnWVmlk3SpUiLCWs6ufVG0hJqiMixpBWVJCqq3pTFmWVJI6ebGN902LUXF2ptc2rFL6V4sBZPWDdSkLK7QlmZZdamKdm8SdxQZlMQVW9LG8ThyVBa1ogxIGf/2YjSxZevP/J8z73w/EEZ6c3TOO0n85c3ROXMcEQIA9L4/6/YEAACtQdABIBEEHQASQdABIBEEHQASQdABIBEE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J7s7q+zKfNAl/Y2k30TEmxGxIenHkp7o8pw6JiLeiYhXt75eV+UPc40fCJ8G24ckfU7S892eS6fZ/rCkT0v6viRFxEZErHV3Vh13QFLO9gFJ90m60eX5ZFYvBH1E0tt3fH9dfRa0KtuHJY1JeqW7M+m470r6lqRb+22YoCOSViVd2Drl9Lzt+7s9qU6JiKKkb0u6JukdSX+IiJ93d1bZ1QtBhyTbH5L0E0lfj4g/dns+nWL785LejYiFbs+lSw5I+oSk70XEmKQ/Seqb3yPZfkCV/yM/IumgpPttP9XdWWVXLwS9KOnhO74/tDXWN2wPqhLzixEx3e35dNi4pC/Y/p0qp9ses/2j7k6po65Luh4R1f8ru6RK4PvFZyT9NiJWI2JT0rSkT3V5TpnVC0H/laSP2j5i+wOq/ELkp12eU8fYtirnT69GxHe6PZ9Oi4ipiDgUEYdV+Xc/FxF9s0KLiBVJb9uuPhn9cUlvdHFKnXZN0qO279v6s/C4+uiXwvXK9EOiJSki3rP9T5JmVfkN9w8i4vUuT6uTxiV9SdKS7StbY/8cEf/RxTmhs74q6eLWguZNSU93eT4dExGv2L4k6VVVrvhaFHeN7oo7RQEgEb1wygUAUAOCDgCJIOgAkAiCDgCJIOgAkAiCDgCJIOgAkAiCDgCJ+H8Qwz4nCLP4ZwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "N = 10\n",
    "y1 = np.logspace(0.1, 1, N, endpoint=True)\n",
    "y2 = np.logspace(0.1, 1, N, endpoint=True)\n",
    "x = np.arange(\n",
    "    0,\n",
    "    10,\n",
    ")\n",
    "plt.plot(x, y1, 'o')\n",
    "plt.plot(x + 0.5, y2, '^')\n",
    "# plt.ylim([0, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float16')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "a.dtype = np.float16\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3., 4.], dtype=float16)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "np.float16(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.dtype([(\"name\", np.str_, 40), (\"numitems\", np.int64), (\"price\",\n",
    "                                                          np.float64)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 单个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11]])\n",
    "arr[0, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "#### 取连续，用切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0, 3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 不连续，用元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  6, 10])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[([0, 1, 2], [1, 2, 3])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  6,  7],\n",
       "       [ 7,  9, 10]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1:, (0, 2, 3)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 4,  5,  6,  7,  8],\n",
       "       [ 7,  8,  9, 10, 11]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = np.array([1, 0, 1], dtype=np.bool)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 改变数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(12)\n",
    "arr.reshape((3, 4))  # 有返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr.resize((3, 4))  # 不返回值，在原数组上该变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 使用ravel函数展平数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(12).reshape(3, 4)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.ravel()  # 原数组不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 横向展平（行优先）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.flatten()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 纵向展平（列优先）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  4,  8,  1,  5,  9,  2,  6, 10,  3,  7, 11])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.flatten('F')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组组合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用hstack函数实现数组横向组合：np.hstack((arr1,arr2))\n",
    "\n",
    "- 使用vstack函数实现数组纵向组合：np.vstack((arr1,arr2))\n",
    "\n",
    "- 使用concatenate函数实现数组横向组合：np.concatenate((arr1,arr2),axis = 1))\n",
    "\n",
    "- 使用concatenate函数实现数组纵向组合：np.concatenate((arr1,arr2),axis = 0))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 切割数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用hsplit函数实现数组横向分割： np.hsplit(arr1, 2)\n",
    "\n",
    "- 使用vsplit函数实现数组纵向分割： np.vsplit(arr, 2)\n",
    "\n",
    "- 使用split函数实现数组横向分割： np.split(arr, 2, axis=1)\n",
    "\n",
    "- 使用split函数实现数组纵向分割： np.split(arr, 2, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 平均分割\n",
    "x = np.arange(9.0)\n",
    "np.split(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2.]),\n",
       " array([3., 4.]),\n",
       " array([5.]),\n",
       " array([6., 7.]),\n",
       " array([], dtype=float64),\n",
       " array([], dtype=float64)]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8.0)\n",
    "np.split(x, [3, 5, 6, 10, 20])  # 分成6份,即使为空"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建与组合矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Interpret the input as a matrix\n",
    "#Equivalent to ``matrix(data, copy=False)\n",
    "matr1 = np.mat(\"1 2 3;4 5 6;7 8 9\")\n",
    "matr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[123],\n",
       "        [456],\n",
       "        [789]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr2 = np.matrix([[123], [456], [789]])\n",
    "matr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 使用bmat函数合成矩阵：（分块矩阵组合）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.mat('1 1; 1 1')\n",
    "B = np.mat('2 2; 2 2')\n",
    "C = np.mat('3 4; 5 6')\n",
    "D = np.mat('7 8; 9 0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 1, 2, 2],\n",
       "        [1, 1, 2, 2],\n",
       "        [3, 4, 7, 8],\n",
       "        [5, 6, 9, 0]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat([[A, B], [C, D]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 1, 2, 2],\n",
       "        [1, 1, 2, 2]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[A, B]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 1, 2, 2],\n",
       "        [1, 1, 2, 2],\n",
       "        [3, 4, 7, 8],\n",
       "        [5, 6, 9, 0]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat(np.r_[np.c_[A, B], np.c_[C, D]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 1, 2, 2],\n",
       "        [1, 1, 2, 2],\n",
       "        [3, 4, 7, 8],\n",
       "        [5, 6, 9, 0]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat('A,B; C,D')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 矩阵与数相乘："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 = np.arange(0, 4).reshape((2, 2))\n",
    "matr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [4, 5]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr2 = np.arange(2, 6).reshape((2, 2))\n",
    "matr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3],\n",
       "       [6, 9]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 矩阵相加减："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [6, 8]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 + matr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 矩阵相乘："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3],\n",
       "       [ 8, 15]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 * matr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 矩阵对应元素相乘："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3],\n",
       "       [ 8, 15]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(matr1, matr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|属性     |说明|\n",
    "|----------|------|\n",
    "|T| 返回自身的转置 |\n",
    "|H| 返回自身的共轭转置|\n",
    "|I|返回自身的逆矩阵|\n",
    "|A|返回自身数据的2维数组的一个视图|\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 认识ufunc函数\n",
    "\n",
    " **全称通用函数（universal function），是一种能够对数组中所有元素进行操作的函数。**\n",
    " \n",
    "-  四则运算：加（+）、减（-）、乘（*）、除（/）、幂（**）。数组间的四则运算表示对每个数组中的元素分别进行四则运算，所以形状必须相同。\n",
    " \n",
    "- 比较运算：>、<、==、>=、<=、!=。比较运算返回的结果是一个布尔数组，每个元素为每个数组对应元素的比较结果。\n",
    "\n",
    "- 逻辑运算：np.any函数表示逻辑“or”，np.all函数表示逻辑“and”。运算结果返回布尔值。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** ufunc函数的广播机制:**\n",
    "\n",
    "广播（broadcasting）是指不同形状的数组之间执行算术运算的方式。需要遵循4个原则。\n",
    "\n",
    "- 让所有输入数组都向其中shape最长的数组看齐，shape中不足的部分都通过在前面加1补齐。\n",
    "- 输出数组的shape是输入数组shape的各个轴上的最大值。\n",
    "- 如果输入数组的某个轴和输出数组的对应轴的长度相同或者其长度为1时，这个数组能够用来计算，否则出错。\n",
    "- 当输入数组的某个轴的长度为1时，沿着此轴运算时都用此轴上的第一组值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读写文件\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**- save函数是以二进制的格式保存数据。 np.save(\"../tmp/save_arr\",arr)**\n",
    "\n",
    "**- load函数是从二进制的文件中读取数据。 np.load(\"../tmp/save_arr.npy\") **\n",
    "\n",
    "**- savez函数可以将多个数组保存到一个文件中。 np.savez('../tmp/savez_arr',arr1,arr2)**\n",
    "\n",
    "**- 存储时可以省略扩展名，但读取时不能省略扩展名。 **"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**savetxt函数是将数组写到某种分隔符隔开的文本文件中。\n",
    "      np.savetxt(\"../tmp/arr.txt\", arr, fmt=\"%d\", delimiter=\",\")**\n",
    "\n",
    "**loadtxt函数执行的是把文件加载到一个二维数组中。\n",
    "      np.loadtxt(\"../tmp/arr.txt\",delimiter=\",\")**\n",
    "\n",
    "**genfromtxt函数面向的是结构化数组和缺失数据。\n",
    "      np.genfromtxt(\"../tmp/arr.txt\", delimiter = \",\")**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 直接排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sort函数是最常用的排序方法。 arr.sort()\n",
    "\n",
    "sort函数也可以指定一个axis参数，使得sort函数可以沿着指定轴对数据集进行排序。axis=1为沿横轴排序； axis=0为沿纵轴排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： [7 4 8 5 7 3 7 8 5 4]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(42)  #设置随机种子\n",
    "arr = np.random.randint(1, 10, size=10)  #生成随机数\n",
    "print('创建的数组为：', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后数组为： [3 4 4 5 5 7 7 7 8 8]\n"
     ]
    }
   ],
   "source": [
    "arr.sort()  #直接排序\n",
    "print('排序后数组为：', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [[9 2 9]\n",
      " [5 2 4]\n",
      " [7 8 3]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randint(1, 10, size=(3, 3))  #生成3行3列的随机数\n",
    "print('创建的数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后数组为：\n",
      " [[2 9 9]\n",
      " [2 4 5]\n",
      " [3 7 8]]\n"
     ]
    }
   ],
   "source": [
    "arr.sort(axis=1)  #沿着横轴排序\n",
    "print('排序后数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后数组为：\n",
      " [[2 4 5]\n",
      " [2 7 8]\n",
      " [3 9 9]]\n"
     ]
    }
   ],
   "source": [
    "arr.sort(axis=0)  #沿着纵轴排序\n",
    "print('排序后数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 间接排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "argsort函数返回值为重新排序值的**下标**。 arr.argsort()\n",
    "\n",
    "lexsort函数返回值是按照最后一个传入数据排序的。 np.lexsort((a,b,c))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([2, 3, 6, 8, 0, 7])\n",
    "print('创建的数组为：\\n', arr)\n",
    "print('排序后数组为：\\n', arr.argsort())  #返回值为重新排序值的下标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [2 3 6 8 0 7]\n",
      "排序后数组为：\n",
      " [4 0 1 2 5 3]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([3, 2, 6, 4, 5])\n",
    "b = np.array([50, 30, 40, 20, 10])\n",
    "c = np.array([400, 300, 600, 100, 200])\n",
    "d = np.lexsort((a, b, c))  #lexsort函数只接受一个参数，即（a,b,c）,返回下标\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([4, 5, 2, 3, 6]),\n",
       " array([20, 10, 30, 50, 40]),\n",
       " array([100, 200, 300, 400, 600]))"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[d], b[d], c[d]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([4, 5, 2, 3, 6]),\n",
       " array([20, 10, 30, 50, 40]),\n",
       " array([100, 200, 300, 400, 600]))"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 相当于\n",
    "a[c.argsort()], b[c.argsort()], c[c.argsort()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后数组为：\n",
      " [(4, 20, 100), (5, 10, 200), (2, 30, 300), (3, 50, 400), (6, 40, 600)]\n"
     ]
    }
   ],
   "source": [
    "#多个键值排序是按照最后一个传入数据计算的\n",
    "print('排序后数组为：\\n', list(zip(a[d], b[d], c[d])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 去重"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### unique函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： ['小明' '小黄' '小花' '小明' '小花' '小兰' '小白']\n"
     ]
    }
   ],
   "source": [
    "names = np.array(['小明', '小黄', '小花', '小明', '小花', '小兰', '小白'])\n",
    "print('创建的数组为：', names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重后的数组为： ['小兰' '小明' '小白' '小花' '小黄']\n"
     ]
    }
   ],
   "source": [
    "print('去重后的数组为：', np.unique(names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重后的数组为： ['小兰', '小明', '小白', '小花', '小黄']\n"
     ]
    }
   ],
   "source": [
    "#跟np.unique等价的Python代码实现过程\n",
    "print('去重后的数组为：', sorted(set(names)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： [ 1  2  3  4  4  5  6  6  7  8  8  9 10]\n",
      "去重后的数组为： [ 1  2  3  4  5  6  7  8  9 10]\n"
     ]
    }
   ],
   "source": [
    "ints = np.array([1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10])  #创建数值型数据\n",
    "print('创建的数组为：', ints)\n",
    "print('去重后的数组为：', np.unique(ints))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### tile函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： [0 1 2 3 4]\n",
      "重复后数组为： [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(5)\n",
    "print('创建的数组为：', arr)\n",
    "print('重复后数组为：', np.tile(arr, 3))  #对数组进行重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.tolist() * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  3,  6,  9, 12])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### repeat函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [[6 3 7]\n",
      " [4 6 9]\n",
      " [2 6 7]]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(42)  #设置随机种子\n",
    "arr = np.random.randint(0, 10, size=(3, 3))\n",
    "print('创建的数组为：\\n', arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重复后数组为：\n",
      " [[6 3 7]\n",
      " [6 3 7]\n",
      " [4 6 9]\n",
      " [4 6 9]\n",
      " [2 6 7]\n",
      " [2 6 7]]\n"
     ]
    }
   ],
   "source": [
    "print('重复后数组为：\\n', arr.repeat(2, axis=0))  #按行进行元素重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重复后数组为：\n",
      " [[6 6 3 3 7 7]\n",
      " [4 4 6 6 9 9]\n",
      " [2 2 6 6 7 7]]\n"
     ]
    }
   ],
   "source": [
    "print('重复后数组为：\\n', arr.repeat(2, axis=1))  #按列进行元素重复"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**这两个函数的主要区别在于，tile函数是对数组进行重复操作，repeat函数是对数组中的每个元素进行重复操作。**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的统计函数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|函数\t|说明|\n",
    "|-------|---------|\n",
    "|sum\t|计算数组的和|\n",
    "|mean\t|计算数组均值|\n",
    "|std\t|计算数组标准差|\n",
    "|var\t|计算数组方差|\n",
    "|min|\t计算数组最小值|\n",
    "|max|\t计算数组最大值|\n",
    "|argmin\t|返回数组最小元素的索引|\n",
    "|argmax\t|返回数组最小元素的索引|\n",
    "|cumsum\t|计算所有元素的累计和|\n",
    "|cumprod\t|计算所有元素的累计积|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： [[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "数组的和为： 190\n",
      "数组横轴的和为： [30 34 38 42 46]\n",
      "数组纵轴的和为： [10 35 60 85]\n",
      "数组的均值为： 9.5\n",
      "数组横轴的均值为： [ 7.5  8.5  9.5 10.5 11.5]\n",
      "数组纵轴的均值为： [ 2.  7. 12. 17.]\n",
      "数组的标准差为： 5.766281297335398\n",
      "数组的方差为： 33.25\n",
      "数组的最小值为： 0\n",
      "数组的最大值为： 19\n",
      "数组的最小元素为： 0\n",
      "数组的最大元素为： 19\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(20).reshape(4,5)\n",
    "print('创建的数组为：',arr)\n",
    "print('数组的和为：',np.sum(arr))  #计算数组的和\n",
    "print('数组横轴的和为：',arr.sum(axis = 0))  #沿着横轴计算求和\n",
    "print('数组纵轴的和为：',arr.sum(axis = 1))  #沿着纵轴计算求和\n",
    "print('数组的均值为：',np.mean(arr))  #计算数组均值\n",
    "print('数组横轴的均值为：',arr.mean(axis = 0))  #沿着横轴计算数组均值\n",
    "print('数组纵轴的均值为：',arr.mean(axis = 1))  #沿着纵轴计算数组均值\n",
    "print('数组的标准差为：',np.std(arr))  #计算数组标准差\n",
    "print('数组的方差为：',np.var(arr))  #计算数组方差\n",
    "print('数组的最小值为：',np.min(arr))  #计算数组最小值\n",
    "print('数组的最大值为：',np.max(arr))  #计算数组最大值\n",
    "print('数组的最小元素为：',np.argmin(arr))  #返回数组最小元素的索引\n",
    "print('数组的最大元素为：',np.argmax(arr))  #返回数组最大元素的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： [2 3 4 5 6 7 8 9]\n",
      "数组元素的累计和为： [ 2  5  9 14 20 27 35 44]\n",
      "数组元素的累计积为： [     2      6     24    120    720   5040  40320 362880]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(2,10)\n",
    "print('创建的数组为：',arr)\n",
    "print('数组元素的累计和为：',np.cumsum(arr))  #计算所有元素的累计和\n",
    "print('数组元素的累计积为：',np.cumprod(arr))  #计算所有元素的累计积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### Nothing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((5, 1, 7), array([[[ 0,  1,  2,  3,  4,  5,  6]],\n",
       " \n",
       "        [[ 7,  8,  9, 10, 11, 12, 13]],\n",
       " \n",
       "        [[14, 15, 16, 17, 18, 19, 20]],\n",
       " \n",
       "        [[21, 22, 23, 24, 25, 26, 27]],\n",
       " \n",
       "        [[28, 29, 30, 31, 32, 33, 34]]]))"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[:, np.newaxis, :]\n",
    "b.shape, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 1)"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "a[:, np.newaxis].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 5)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.newaxis, :].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[ 0,  1,  2],\n",
       "         [ 3,  4,  5],\n",
       "         [ 6,  7,  8]],\n",
       "\n",
       "        [[ 9, 10, 11],\n",
       "         [12, 13, 14],\n",
       "         [15, 16, 17]],\n",
       "\n",
       "        [[18, 19, 20],\n",
       "         [21, 22, 23],\n",
       "         [24, 25, 26]]],\n",
       "\n",
       "\n",
       "       [[[27, 28, 29],\n",
       "         [30, 31, 32],\n",
       "         [33, 34, 35]],\n",
       "\n",
       "        [[36, 37, 38],\n",
       "         [39, 40, 41],\n",
       "         [42, 43, 44]],\n",
       "\n",
       "        [[45, 46, 47],\n",
       "         [48, 49, 50],\n",
       "         [51, 52, 53]]],\n",
       "\n",
       "\n",
       "       [[[54, 55, 56],\n",
       "         [57, 58, 59],\n",
       "         [60, 61, 62]],\n",
       "\n",
       "        [[63, 64, 65],\n",
       "         [66, 67, 68],\n",
       "         [69, 70, 71]],\n",
       "\n",
       "        [[72, 73, 74],\n",
       "         [75, 76, 77],\n",
       "         [78, 79, 80]]]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(81).reshape(3, 3, 3, 3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[29 32 35]\n",
      " [38 41 44]\n",
      " [47 50 53]] \n",
      "----\n",
      " [[29 32 35]\n",
      " [38 41 44]\n",
      " [47 50 53]]\n"
     ]
    }
   ],
   "source": [
    "print(a[1, ..., 2], '\\n----\\n', a[1, :, :, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(6)\n",
    "a.shape = (2, 1, 3)\n",
    "a.squeeze()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[11, 17,  3, 11,  4],\n",
       "        [ 7, 18,  5,  2, 14],\n",
       "        [ 1,  6, 19, 16, 11],\n",
       "        [ 1, 16,  0, 19,  1]],\n",
       "\n",
       "       [[ 9,  5, 14, 13, 16],\n",
       "        [ 2, 12, 19, 12, 18],\n",
       "        [16,  1,  2,  8,  6],\n",
       "        [ 5, 10, 10,  5, 14]],\n",
       "\n",
       "       [[10, 12,  7,  1,  1],\n",
       "        [ 2,  7, 14, 17, 15],\n",
       "        [15, 19,  5, 11,  1],\n",
       "        [11, 19,  7,  6, 18]]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.randint(20, size=60).reshape(3, 4, 5)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1, 1, 1],\n",
       "       [0, 0, 1, 2, 1],\n",
       "       [1, 2, 0, 0, 0],\n",
       "       [2, 2, 1, 0, 2]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(x, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0]\n",
      " [10]\n",
      " [20]\n",
      " [30]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0],\n",
       "       [10, 10, 10],\n",
       "       [20, 20, 20],\n",
       "       [30, 30, 30]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(0, 40, 10)\n",
    "\n",
    "a = a[:, np.newaxis]  # 转换a的维度（形状）\n",
    "print(a)\n",
    "np.array(([i * 3 for i in a.tolist()]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0], [10], [20], [30], [0], [10], [20], [30], [0], [10], [20], [30]]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tolist() * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 1, 2, 3, 1, 2, 3]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "a * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([('', 18, 2000.), ('', 23, 4000.)],\n",
       "      dtype=[('name', '<U'), ('age', '<i8'), ('incom', '<f8')])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array(\n",
    "    [('Bob', 18, 2000.0), ('Tom', 23, 4000.0)],\n",
    "    dtype=[('name', np.str_), ('age', np.int_), ('incom', np.float_)])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('', 18, 2000.)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import numpy.ma as ma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "masked_array(\n",
       "  data=[[1, --],\n",
       "        [--, 4]],\n",
       "  mask=[[False,  True],\n",
       "        [ True, False]],\n",
       "  fill_value=999999)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ma.array([[1, 2], [3, 4]], mask=[[0, 1], [1, 0]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### [掩码](http://www.naodongopen.com/290.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "masked_array(\n",
       "  data=[[1, --],\n",
       "        [--, 4]],\n",
       "  mask=[[False,  True],\n",
       "        [ True, False]],\n",
       "  fill_value=999999)"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = ma.masked_array([[1, 2], [3, 4]], mask=[[0, 1], [1, 0]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "masked_array(data=[1, 4],\n",
       "             mask=[False, False],\n",
       "       fill_value=999999)"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[~a.mask]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.compressed()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "masked_array(data=[0, 1, --, --, --],\n",
       "             mask=[False, False,  True,  True,  True],\n",
       "       fill_value=999999)"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.ma.array(np.arange(5), mask=[0] * 2 + [1] * 3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1])"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.compressed()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [修改掩码](http://www.naodongopen.com/290.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### genfromtxt函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from io import BytesIO\n",
    "data = \"1, 2, 3\\n4, 5, 6\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "1\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "#StringIO:在内存中读写str\n",
    "from io import StringIO\n",
    "f = StringIO()\n",
    "\n",
    "print(f.write('hello'))\n",
    "print(f.write(' '))\n",
    "print(f.write('world!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world!'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.getvalue()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "433px",
    "left": "991px",
    "top": "110px",
    "width": "279px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
